package JVM;

public class Signal {
    public static  int signal=0; //这里有点不懂，为什么去掉了volatile，就会一直阻塞

    public static void main(String[] args) throws InterruptedException {
        Object o=new Object();
        Thread t1=new Thread(()->{
            synchronized (o){
                while (signal<5){
                    if ((signal&1)==1){
                    System.out.println("t1:"+signal);
                    signal++;
                    o.notify();
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }else {
                        o.notify();
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        Thread t2=new Thread(()->{
            synchronized (o){
                while (signal<5){
                    if ((signal&1)==0){
                    System.out.println("t2:"+signal);
                    signal++;
                    o.notify();
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }else {
                        o.notify();
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        t1.start();
        Thread.sleep(1000);
        t2.start();

    }
}
